package cn.anecansaitin.whimsy.common.extension.comboSystem;

import net.minecraft.world.entity.player.Player;

public class Combo {
    private final SkillGraph skills;
    private final SkillPerformer performer;
    private int currentIndex = 0;

    public Combo(SkillGraph skills, SkillPerformer performer) {
        this.skills = skills;
        this.performer = performer;
    }

    public void tick(Player player) {
        SkillPerformer.ExecuteState state = performer.tick(player);

        switch (state.state()) {
            case EXECUTING -> {
            }
            case NEXT -> deriveSkill(state.key());
            case FINISHED -> deriveEnd();
        }
    }

    public void putExtraData(String name, Object data) {
        performer.putExtraData(name, data);
    }

    public void inputKey(int key) {
        performer.inputKey(key);
    }

    public ISkill getCurrentSkill() {
        return performer.getSkill();
    }

    private void deriveSkill(int key) {
        SkillGraph.Result result = skills.getDeriveIndex(currentIndex, key);

        switch (result.state()) {
            case FOUND -> {
                currentIndex = result.index();
                performer.setSkill(result.skill(), key);
            }
            case NOT_FOUND -> {
            }
        }
    }

    private void deriveEnd() {
        SkillGraph.Result result = skills.getEmpty();
        currentIndex = result.index();
        performer.setSkill(result.skill(), 0);
    }
}
